# 实现一个装饰器，login_required
# 如果需要调用被装饰的函数，你需要已经登录了
# @login_required
# def index():pass
# 1. 全局变量：user = None => 未登录：打印“请先登录再访问”,不调用index
#               not None =>  已经登录：调用index
# 2. 将用户数据保存到文件，从文件中读取用户信息。
user = None
def login_required(func):
    def inner(*args, **kwargs):
        global user
        if user is None:
            return "请先登录再访问！"
        else:
            result = func(*args, **kwargs)
            return result
    return inner

@login_required
def checkLogin():
    print("this is index test")
    return "访问index.html成功"

def readUser(username):
    global user
    with open("user.txt", "r", encoding="utf-8") as fp:
        users = fp.readlines()
        user = [i.strip() for i in users]
        if username in user:
            print(f"{username}登录成功")
        else:
            print(f"{username}不存在")

print(checkLogin())
readUser("张三")
print(checkLogin())


